<?php
/**
 * Created by PhpStorm.
 * User: xutao
 * Date: 2018/11/9
 * Time: 17:57
 */
//权限列表
namespace app\admin\controller;



use app\admin\model\AuthRules;

class Rules extends Base{



    //列表
    public function index(){
        $AuthRules = AuthRules::get_all();
        $this->assign('authrules',$AuthRules);
        return $this->fetch();
    }

    //添加
    public function add(){
        if(request()->isPost()){
            $data = input('post.');
            $AuthRules = new AuthRules();
            return $AuthRules->add($data);
        }else{
            $AuthRules = AuthRules::get_all();
            $this->assign('authrules',$AuthRules);
            return $this->fetch();
        }
    }
    //修改状态
    public function set_status(){
        if(!request()->isPost()) return json(['code'=>0,'data'=>'','msg'=>'非法请求']);
        $status = input('param.status');
        $id = input('param.id');
        $re = AuthRules::where('id',$id)->update([
            'status'=>$status
        ]);
        if(!$re)  return json(['code'=>1,'data'=>'','msg'=>'修改失败']);
        return json(['code'=>200,'data'=>'','msg'=>'修改成功']);
    }

    //删除
    public function del(){
        if(!request()->isPost()) return json(['code'=>0,'data'=>'','msg'=>'非法请求']);
        $id = input('param.');
                AuthRules::destroy($id);
        return json(['code'=>200,'data'=>$id,'msg'=>'ok']);
    }

    //编辑
    public function edit(){
        if(request()->isPost()){
            $data = input('post.');
            $AuthRules = new AuthRules();
            return $AuthRules->edit($data);
        }else{
            $id = input('id');
            if(!$id) return json(['code'=>1,'data'=>'','msg'=>'参数错误']);
            $data = AuthRules::get($id);
            $AuthRules = AuthRules::get_all();
            $this->assign('data',$data);
            $this->assign('authrules',$AuthRules);
            return $this->fetch();
        }



    }


}